//
//  VC6Animation.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/26.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC6Animation: UIViewController {
    
    var colorView:UIView!
    var colorView2:UIView!
    var imgview:UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        colorView = UIView(frame: CGRect(x: 0, y: 100, width: 100, height: 100))
        colorView.backgroundColor = UIColor.red
        
        colorView2 = UIView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
        colorView2.backgroundColor = UIColor.blue
        view.addSubview(colorView)
        view.addSubview(colorView2)
        
        imgview = UIImageView(image: UIImage(named: "ic_action_home")!)
        imgview.center = view.center
        view.addSubview(imgview)
        
        colorView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickColorView)))
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        test9()
    }
    
    func test9() -> Void {
        colorView.layer.anchorPoint = CGPoint(x: 0, y: 0)
        colorView.layer.transform = CATransform3DRotate(colorView.layer.transform, 20, 0, 0, 1)
    }
    
    func test8() -> Void {
        UIView.transition(from: colorView, to: colorView2, duration: 3, options: [.transitionFlipFromLeft,.allowAnimatedContent]) { (finish) in
            print("完成")
        }
    }
    
    func test7() -> Void {
        UIView.transition(with: self.colorView, duration: 3, options: .allowAnimatedContent, animations: {
            self.colorView.backgroundColor = UIColor.blue
        }) { (finish) in
            print("完成")
        }
    }
    
    func test6() -> Void {
        UIView.animate(withDuration: 3) {
            self.colorView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
        }
    }
    
    func test5() -> Void {
        UIView.animate(withDuration: 3, animations: {
            self.colorView.frame = CGRect(x: 0, y: 200, width: 100, height: 100)
        }) { (finish) in
            UIView.animate(withDuration: 2, animations: {
                self.colorView.backgroundColor = UIColor.blue
                self.colorView.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
            }) { (finish) in
                print("完成")
            }
        }
    }
    
    func test4() -> Void {
        UIView.animate(withDuration: 3, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 20, options: .transitionCurlUp, animations: {
            self.colorView.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
        }) { (finish) in
            print("完成")
        }
    }
    
    func test3() -> Void {
        UIView.animate(withDuration: 3, delay: 1, options: .curveEaseIn, animations: {
            self.colorView.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
        }) { (finish) in
            print("完成")
        }
    }
    
    func test2() -> Void {
        UIView.animate(withDuration: 3, animations: {
            self.colorView.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
        }) { (finish) in
            print("完成")
        }
    }
    
    func test1() -> Void {
        UIView.animate(withDuration: 3) {
            self.colorView.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
        }
    }
    
    @objc func clickColorView() -> Void {
        print("click")
    }
}
